﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.EducationAndTraining
{
    public class RollCalls : DetailView<rollcall>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<rollcall> EntityList;
        private new rollcall Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<rollcall>

        public override List<rollcall> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.rollcalls.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<rollcall> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.rollcalls.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override rollcall GetEntity()
        {
            _context = new project5188Entities();
            Entity = new rollcall();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTorollcalls(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<rollcall> GetEntityListByParameters(DateTime? date, string studentName, string tcNoText, string coursePeriod, string courseCode, bool? isAttendance)
        {
            _context = new project5188Entities();

            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            dynamicString += " And it.studentregistry.IsDeleted = @StudentIsDeleted";
            parameterList.Add(new ObjectParameter("StudentIsDeleted", false));

            dynamicString += " And it.timetable.IsDeleted = @TimeTableIsDeleted";
            parameterList.Add(new ObjectParameter("TimeTableIsDeleted", false));

            dynamicString += " And it.timetable.course.IsDeleted = @CourseIsDeleted";
            parameterList.Add(new ObjectParameter("CourseIsDeleted", false));

            if (date != null)
            {
                dynamicString += " And it.d_Date = @Date";
                parameterList.Add(new ObjectParameter("Date", date));
            }

            if (!string.IsNullOrEmpty(studentName) && !string.IsNullOrWhiteSpace(studentName))
            {
                dynamicString += " And it.studentregistry.e_NameSurname = @StudentName";
                parameterList.Add(new ObjectParameter("StudentName", studentName));
            }

            if (!String.IsNullOrEmpty(tcNoText) && !String.IsNullOrWhiteSpace(tcNoText))
            {
                dynamicString += " And it.studentregistry.f_TCNo = @TCNo";
                parameterList.Add(new ObjectParameter("TCNo", Convert.ToInt32(tcNoText)));
            }

            if (!String.IsNullOrEmpty(coursePeriod) && !String.IsNullOrWhiteSpace(coursePeriod))
            {
                dynamicString += " And it.timetable.d_CoursesPeriod = @CoursePeriod";
                parameterList.Add(new ObjectParameter("CoursePeriod", coursePeriod));
            }

            if (!String.IsNullOrEmpty(courseCode) && !String.IsNullOrWhiteSpace(courseCode))
            {
                dynamicString += " And it.timetable.course.e_CourseCode = @CourseCode";
                parameterList.Add(new ObjectParameter("CourseCode", courseCode));
            }

            if (isAttendance != null)
            {
                dynamicString += " And it.e_IsAttendance = @IsAttendance";
                parameterList.Add(new ObjectParameter("IsAttendance", isAttendance));
            }

            EntityList = _context.rollcalls.Where(dynamicString, parameterList.ToArray()).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }
    }
}
